<?php
/**
 * Created by PhpStorm.
 * User: YAN
 * Date: 2017/7/24
 * Time: 9:33
 */

namespace App\Components;
use Thenbsp\Wechat\Message\Template\Sender;
use Thenbsp\Wechat\Message\Template\Template;
use Thenbsp\Wechat\Wechat\AccessToken;

class getUsersWeiXinInfo
{
    private $open_path = 'https://api.weixin.qq.com/sns/oauth2/access_token';
    private $token_path = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential';
    private $secret='27074f29de1d2ef03f1d697ad53379c9';
    private $appid='wxa809db2a64aa8ecd';
//    private $secret='39d2a8b87faa4fc13f6c943c54d6d8ed';
//    private $appid='wx0f7b837091cb7268';
//'app_id'  => env('WECHAT_APPID', 'your-app-id'),         // AppID
//'secret'  => env('WECHAT_SECRET', 'your-app-secret'),
    private $grant_type='authorization_code';
    const TOKEN_KEY = 'weixin_access_tokens';

    public function __construct()
    {
        $this->appid = env('WECHAT_APPID');
        $this->secret = env('WECHAT_SECRET');
    }

    public function getInfo($code){
        $url=$this->open_path.'?appid='.$this->appid.'&secret='.$this->secret.'&code='.$code.'&grant_type='.$this->grant_type;
        $html = file_get_contents($url);
        return $html;
    }

    public function getAccessToken()
    {
        if (!\Cache::get(self::TOKEN_KEY)) {
            $token =self::getToken();
            \Cache::put(self::TOKEN_KEY, json_encode($token, JSON_UNESCAPED_UNICODE),3600);
        }
        $cache = \Cache::get(self::TOKEN_KEY);
        return json_decode($cache, true);
    }
    public function getToken(){
        $url=$this->token_path.'&appid='.$this->appid.'&secret='.$this->secret;
        $html =\GuzzleHttp\json_decode(file_get_contents($url)) ;
        //var_dump($html);die;
        return $html->access_token;
    }
    public function sendMessage($open_id,$contect){

        $post_data['touser']=$open_id;
        $post_data['msgtype']='text';
        $post_data['text']['content']=$contect;
       // var_dump($post_data);die;
        $url ='https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$this-> getAccessToken();
   //     var_dump(http_build_query($post_data));die;
        $output=$this->file_get_contents_post($url,$post_data);
        return $output;
    }
    public function endTMessage($open_id,$url,$title,$user,$time,$place,$desc){
        $post_data['touser']=$open_id;
        $post_data['template_id']='tDxdgiSs0elWnQDwsX3tGXFdkxgnqTMBprt5Rmr7gzw';
        $post_data['url']=$url;
        $post_data['data']['first']=['value'=>$title,'color'=>'#173177'];
        $post_data['data']['keyword1']=['value'=>$user,'color'=>'#173177'];
        $post_data['data']['keyword2']=['value'=>$time,'color'=>'#173177'];
        $post_data['data']['keyword3']=['value'=>$place,'color'=>'#173177'];
        $post_data['data']['remark']=['value'=>$desc,'color'=>'#173177'];
        $url ='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->getAccessToken();
        $output=$this->file_get_contents_post($url,$post_data);
        return $output;
    }
    public function getUserInfo($key){

    }
    public function file_get_contents_post($url, $post){
        $options = array(
            'http'=> array(
                'method' => 'POST',
                'header' => "Content-type: application/json",
                'content' => json_encode($post,JSON_UNESCAPED_UNICODE),
            ),
        );
        $result = file_get_contents($url,false, stream_context_create($options));
        return $result;
    }
}
